﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;


//Unity Editor 環境 用 MonoBridge 和 c++ 通訊
public interface IBridge
{
    bool CPPDLL_Load(string dllname);
    void CPPDLL_Event(Int64 msgid, Int64 param1, IntPtr param2);
    void CPPDLL_CallUpdate();
    void CPPDLL_Free();
}

public partial class ILBridge
{
    public enum LogLevel
    {
        Info,
        Warn,
        Err,
    }
    [Flags]
    public enum EnvTag
    {
        MONO=0x0100,
        IL2CPP =0x0200,
    }
    public static event Action<string, LogLevel> logCallback;
    public static void Log(string str, LogLevel level = LogLevel.Info)
    {
        if (logCallback != null)
            logCallback.Invoke(str, level);
    }
#if ENABLE_MONO
    IBridge bridge = new MonoBridge();
#else 
    IBridge bridge = new IL2CppBridge();
#endif

    public bool CPPDLL_Load(string dllname)
    {
        return bridge.CPPDLL_Load(dllname);
    }
    public void CPPDLL_Event(Int64 msgid, Int64 param1, IntPtr param2)
    {
        bridge.CPPDLL_Event(msgid, param1, param2);
    }

    public void CPPDLL_CallUpdate()
    {
        bridge.CPPDLL_CallUpdate();
    }
    public void CPPDLL_Free()
    {
        bridge.CPPDLL_Free();
    }
    internal static Type _FindType(string name)
    {
        var assemblies = AppDomain.CurrentDomain.GetAssemblies();
        Type t = null;
        foreach (var ass in assemblies)
        {
            var gt = ass.GetType(name);
            if (gt != null)
            {
                t = gt;
                break;
            }
        }
        if (t == null)
        {
            UnityEngine.Debug.LogWarning("_FindType Error." + name);
        }
        return t;
    }
}

